package com.jiang.专题.数组.Q134;

/**
 * @author Jiang
 * @version 1.0
 * @date 2025/01/15 23:23
 */
class Solution {
    public int canCompleteCircuit(int[] gas, int[] cost) {
        int len = gas.length, residue = 0, cur = 0, start = 0;
        for(int i = 0; i < len; i++) {
            cur += gas[i] - cost[i];
            residue += gas[i] - cost[i];
            if (cur < 0) {
                cur = 0;
                start = i + 1;
            }
        }
        if (residue < 0) return -1;
        return start;
    }
}
